﻿Public Class frmIRBoxes
    Private da As New DSVaultingTableAdapters.ir_issue_room_boxesTableAdapter
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private slcID As Long
    Private ccy As String() = {"ប្រាក់រៀល", "ប្រាក់ដុល្លារ"}

    Private Sub frmIRBoxes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DSVaulting.ir_issue_rooms' table. You can move, or remove it, as needed.
        Me.Ir_issue_roomsTableAdapter.Fill(Me.DSVaulting.ir_issue_rooms)
        uxCurrencyCbx.SelectedIndex = 0
        uxIRBoxDgv.DataSource = da.GetDataWithRoomName
    End Sub

    Private Sub uxNewBoxBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxNewBoxBtn.Click
        uxBoxNameTbx.Enabled = True
        uxBoxNameTbx.Text = ""
        action = actType(1)
    End Sub

    Private Sub uxEditBoxBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditBoxBtn.Click
        If uxIRBoxDgv.SelectedRows.Count > 0 Then
            uxBoxNameTbx.Text = uxIRBoxDgv.SelectedRows.Item(0).Cells("BoxName").Value
            uxRoomNameCbx.Text = uxIRBoxDgv.SelectedRows.Item(0).Cells("RoomName").Value
            uxCurrencyCbx.Text = uxIRBoxDgv.SelectedRows.Item(0).Cells("Currency").Value
            If Not uxBoxNameTbx.Text = "" Then
                slcID = Long.Parse(uxIRBoxDgv.SelectedRows.Item(0).Cells("Index").Value)
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxDeleteBoxBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteBoxBtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId = Long.Parse(uxIRBoxDgv.SelectedRows.Item(0).Cells("Index").Value)
            da.Delete(oldId)
            action = actType(3)
            Me.updateDGV()
        End If
    End Sub

    Private Sub uxSaveChangeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSaveChangeBtn.Click
        Dim boxName As String = uxBoxNameTbx.Text
        Dim roomId As Long = Long.Parse(uxRoomNameCbx.SelectedValue)
        If Not boxName = "" Then
            If action = actType(1) Then
                da.Insert(roomId, uxCurrencyCbx.Text, boxName)
                uxBoxNameTbx.Clear()
                updateDGV()
            ElseIf action = actType(2) Then
                If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    da.Update(roomId, uxCurrencyCbx.Text, boxName, slcID)
                    updateDGV()
                End If
            End If
        End If
    End Sub

    Private Sub uxCancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelBtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    Private Sub frmIRBoxes_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub updateDGV()
        uxIRBoxDgv.DataSource = da.GetDataWithRoomName
    End Sub

    Private Sub uxIRBoxDgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxIRBoxDgv.CellDoubleClick
        uxEditBoxBtn_Click(sender, e)
    End Sub
End Class